home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / etc / init.d / rc < prev    next >
Text File  |  2008-10-14  |  10KB  |  386 lines

  1. #! /bin/sh
  2. #
  3. # rc
  4. #
  5. # Starts/stops services on runlevel changes.
  6. #
  7. # Optimization: A start script is not run when the service was already
  8. # configured to run in the previous runlevel.  A stop script is not run
  9. # when the the service was already configured not to run in the previous
  10. # runlevel.
  11. #
  12. # Authors:
  13. #     Miquel van Smoorenburg <miquels@cistron.nl>
  14. #     Bruce Perens <Bruce@Pixar.com>
  15.  
  16. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  17. export PATH
  18.  
  19. # Un-comment the following for debugging.
  20. # debug=echo
  21.  
  22. # Specify method used to enable concurrent init.d scripts.
  23. # Valid options are 'none', 'shell' and 'startpar'.  To enable the
  24. # concurrent boot option, the init.d script order must allow for
  25. # concurrency.  This is not the case with the default boot sequence in
  26. # Debian as of 2008-01-20.  Before enabling concurrency, one need to
  27. # check the sequence values of all boot scripts, and make sure only
  28. # scripts that can be started in parallel have the same sequence
  29. # number, and that a scripts dependencies have a earlier sequence
  30. # number. See the insserv package for a away to reorder the boot
  31. # automatically to allow this.
  32. CONCURRENCY=none
  33.  
  34. # Make sure the name survive changing the argument list
  35. scriptname="$0"
  36.  
  37. umask 022
  38.  
  39. on_exit() {
  40.     echo "error: '$scriptname' exited outside the expected code flow."
  41. }
  42. trap on_exit EXIT # Enable emergency handler
  43.  
  44. # Ignore CTRL-C only in this shell, so we can interrupt subprocesses.
  45. trap ":" INT QUIT TSTP
  46.  
  47. # Set onlcr to avoid staircase effect.
  48. stty onlcr 0>&1
  49.  
  50. # Functions for splash progress bars
  51. if [ -e /lib/init/splash-functions-base ] ; then
  52.     . /lib/init/splash-functions-base
  53. else
  54.     # Quiet down script if old initscripts version without /lib/init/splash-functions-base is used.
  55.     splash_progress() { return 1; }
  56. fi
  57.  
  58. # Now find out what the current and what the previous runlevel are.
  59.  
  60. runlevel=$RUNLEVEL
  61. # Get first argument. Set new runlevel to this argument.
  62. [ "$1" != "" ] && runlevel=$1
  63. if [ "$runlevel" = "" ]
  64. then
  65.     echo "Usage: $scriptname <runlevel>" >&2
  66.     exit 1
  67. fi
  68. previous=$PREVLEVEL
  69. [ "$previous" = "" ] && previous=N
  70.  
  71. export runlevel previous
  72.  
  73. . /etc/default/rcS
  74. export VERBOSE
  75.  
  76. if [ -f /lib/lsb/init-functions ] ; then
  77.     . /lib/lsb/init-functions
  78. else
  79.     log_action_msg() { echo $@; }
  80. fi
  81.  
  82. #
  83. # Stub to do progress bar ticks (for splash programs) on startup
  84. #
  85. startup_progress() {
  86.     # Avoid divide by zero if anyone moved xdm/kdm/gdm first in a runlevel.
  87.     if [ 0 -eq "$num_steps" ] ; then return; fi
  88.  
  89.     step=$(($step + $step_change))
  90.     progress=$(($step * $progress_size / $num_steps + $first_step))
  91.     $debug splash_progress "$progress" || true
  92. }
  93.  
  94. sh=sh
  95. # Debian Policy ┬º9.3.1 requires .sh scripts in runlevel S to be
  96. # sourced However, some important packages currently contain .sh
  97. # scripts that do "exit" at some point, thus killing this process and
  98. # the boot.  Bad!  See also bug #339955.
  99. #[ S = "$runlevel" ] && sh=.
  100.  
  101. #
  102. # Check if we are able to use make like booting.  It require the
  103. # insserv package to be enabled.
  104. #
  105. if [ startpar = "$CONCURRENCY" ] ; then
  106.     test -s /etc/init.d/.depend.boot  || CONCURRENCY="none"
  107.     test -s /etc/init.d/.depend.start || CONCURRENCY="none"
  108.     test -s /etc/init.d/.depend.stop  || CONCURRENCY="none"
  109.     startpar -v      > /dev/null 2>&1 || CONCURRENCY="none"
  110.  
  111.     # startpar do not work properly at the start of rcS.d/.  Avoid it.
  112.     # See #457896 for details.
  113.  
  114.     if [ S = "$runlevel" ] ; then
  115.     CONCURRENCY=none
  116.     fi
  117. fi
  118.  
  119. #
  120. # Start script or program.
  121. #
  122. case "$CONCURRENCY" in
  123.   shell)
  124.       log_action_msg "Using shell-style concurrent boot in runlevel $runlevel"
  125.     startup() {
  126.         action=$1
  127.         shift
  128.         scripts="$@"
  129.         backgrounded=0
  130.         for script in $scripts ; do
  131.             case "$script" in
  132.               *.sh)
  133.                 if [ "." = "$sh" ] ; then
  134.                     RC_SAVE_PATH="$PATH"
  135.                     set -- "$action"
  136.                     $debug . "$script"
  137.                     PATH="$RC_SAVE_PATH"
  138.                     startup_progress
  139.                 else
  140.                     $debug $sh "$script" $action
  141.                     startup_progress
  142.                 fi
  143.                 ;;
  144.               *)
  145.                 $debug "$script" $action &
  146.                 startup_progress
  147.                 backgrounded=1
  148.                 ;;
  149.             esac
  150.         done
  151.         [ 1 = "$backgrounded" ] && wait
  152.     }
  153.     ;;
  154.   startpar)
  155.       log_action_msg "Using startpar-style concurrent boot in runlevel $runlevel"
  156.     startup() {
  157.         action=$1
  158.         shift
  159.         scripts="$@"
  160.         # Make sure .sh scripts are sourced in runlevel S
  161.         if [ "." = "$sh" ] ; then
  162.             newscripts=
  163.             for script in $scripts ; do
  164.                 case "$script" in
  165.                   *.sh)
  166.                     RC_SAVE_PATH="$PATH"
  167.                     set -- "$action"
  168.                     $debug . "$script"
  169.                     PATH="$RC_SAVE_PATH"
  170.                     startup_progress
  171.                     ;;
  172.                   *)
  173.                     newscripts="$newscripts $script"
  174.                     step=$(($step + $step_change))
  175.                     ;;
  176.                 esac
  177.             done
  178.             scripts="$newscripts"
  179.         else
  180.             # Update progress bar counter and jump to the new position
  181.             for script in $scripts ; do
  182.                 step=$(($step + $step_change))
  183.             done
  184.         fi
  185.  
  186.         # startpar is not able to handle time jumps.  So the
  187.         # hwclock.sh scripts should not be executed from
  188.         # within startpar.  The .sh hack above make this
  189.         # problem irrelevant. [pere 2005-09-10]
  190.         [ -n "$scripts" ] && $debug startpar -a $action $scripts
  191.  
  192.         # Jump back one step to compencate for stepping one
  193.         # time too many in the for loop, and to keep the same
  194.         # location as the startup_progress call in the *.sh
  195.         # case.
  196.         step=$(($step - $step_change))
  197.         startup_progress
  198.     }
  199.     ;;
  200.   none|*)
  201.     startup() {
  202.         action=$1
  203.         shift
  204.         scripts="$@"
  205.         for script in $scripts ; do
  206.             case "$script" in
  207.               *.sh)
  208.                 if [ "." = "$sh" ] ; then
  209.                     RC_SAVE_PATH="$PATH"
  210.                     set "$action"
  211.                     $debug . "$script"
  212.                     PATH="$RC_SAVE_PATH"
  213.                     startup_progress
  214.                 else
  215.                     $debug $sh "$script" $action
  216.                     startup_progress
  217.                 fi
  218.                 ;;
  219.               *)
  220.                 $debug "$script" $action
  221.                 startup_progress
  222.                 ;;
  223.             esac
  224.         done
  225.     }
  226.     ;;
  227. esac
  228.  
  229. # Is there an rc directory for this new runlevel?
  230. if [ -d /etc/rc$runlevel.d ]
  231. then
  232.     # Find out where in the progress bar the initramfs got to.
  233.     PROGRESS_STATE=0
  234.     if [ -f /dev/.initramfs/progress_state ]; then
  235.         . /dev/.initramfs/progress_state
  236.     fi
  237.  
  238.     # Split the remaining portion of the progress bar into thirds
  239.     progress_size=$(((100 - $PROGRESS_STATE) / 3))
  240.  
  241.     case "$runlevel" in
  242.         0|6)
  243.             ACTION=stop
  244.             # Count down from 0 to -100 and use the entire bar
  245.             first_step=0
  246.             progress_size=100
  247.             step_change=-1
  248.             ;;
  249.             S)
  250.                 ACTION=start
  251.             # Begin where the initramfs left off and use 2/3
  252.             # of the remaining space
  253.             first_step=$PROGRESS_STATE
  254.             progress_size=$(($progress_size * 2))
  255.             step_change=1
  256.             ;;
  257.         *)
  258.             ACTION=start
  259.             # Begin where rcS left off and use the final 1/3 of
  260.             # the space (by leaving progress_size unchanged)
  261.             first_step=$(($progress_size * 2 + $PROGRESS_STATE))
  262.             step_change=1
  263.             ;;
  264.     esac
  265.  
  266.     # Count the number of scripts we need to run
  267.     # (for progress bars)
  268.     num_steps=0
  269.     for s in /etc/rc$runlevel.d/[SK]*; do
  270.         case "${s##/etc/rc$runlevel.d/S??}" in
  271.          gdm|xdm|kdm|ltsp-client|ltsp-client-core|reboot|halt)
  272.         break
  273.         ;;
  274.         esac
  275.         num_steps=$(($num_steps + 1))
  276.     done
  277.     step=0
  278.  
  279.     # First, run the KILL scripts.
  280.     if [ "$previous" != N ]
  281.     then
  282.         # Run all scripts with the same level in parallel
  283.         CURLEVEL=""
  284.         for s in /etc/rc$runlevel.d/K*
  285.         do
  286.             # Extract order value from symlink
  287.             level=${s#/etc/rc$runlevel.d/K}
  288.             level=${level%%[a-zA-Z]*}
  289.             if [ "$level" = "$CURLEVEL" ]
  290.             then
  291.                 continue
  292.             fi
  293.             CURLEVEL=$level
  294.             SCRIPTS=""
  295.             for i in /etc/rc$runlevel.d/K$level*
  296.             do
  297.                 # Check if the script is there.
  298.                 [ ! -f $i ] && continue
  299.  
  300.                 #
  301.                 # Find stop script in previous runlevel but
  302.                 # no start script there.
  303.                 #
  304.                 suffix=${i#/etc/rc$runlevel.d/K[0-9][0-9]}
  305.                 previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
  306.                 previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
  307.                 #
  308.                 # If there is a stop script in the previous level
  309.                 # and _no_ start script there, we don't
  310.                 # have to re-stop the service.
  311.                 #
  312.                 [ -f $previous_stop ] && [ ! -f $previous_start ] && continue
  313.  
  314.                 # Stop the service.
  315.                 SCRIPTS="$SCRIPTS $i"
  316.             done
  317.             startup stop $SCRIPTS
  318.         done
  319.     fi
  320.  
  321.     # Now run the START scripts for this runlevel.
  322.     # Run all scripts with the same level in parallel
  323.     CURLEVEL=""
  324.     for s in /etc/rc$runlevel.d/S*
  325.     do
  326.         # Extract order value from symlink
  327.         level=${s#/etc/rc$runlevel.d/S}
  328.         level=${level%%[a-zA-Z]*}
  329.         if [ "$level" = "$CURLEVEL" ]
  330.         then
  331.             continue
  332.         fi
  333.         CURLEVEL=$level
  334.         SCRIPTS=""
  335.         for i in /etc/rc$runlevel.d/S$level*
  336.         do
  337.             [ ! -f $i ] && continue
  338.  
  339.             if [ "$previous" != N ]
  340.             then
  341.                 #
  342.                 # Find start script in previous runlevel and
  343.                 # stop script in this runlevel.
  344.                 #
  345.                 suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]}
  346.                 stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffix
  347.                 previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
  348.                 #
  349.                 # If there is a start script in the previous level
  350.                 # and _no_ stop script in this level, we don't
  351.                 # have to re-start the service.
  352.                 #
  353.                 if [ start = "$ACTION" ] ; then
  354.                     [ -f $previous_start ] && [ ! -f $stop ] && continue
  355.                 else
  356.                     # Workaround for the special
  357.                     # handling of runlevels 0 and 6.
  358.                     previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
  359.                     #
  360.                     # If there is a stop script in the previous level
  361.                     # and _no_ start script there, we don't
  362.                     # have to re-stop the service.
  363.                     #
  364.                     [ -f $previous_stop ] && [ ! -f $previous_start ] && continue
  365.                 fi
  366.  
  367.             fi
  368.             SCRIPTS="$SCRIPTS $i"
  369.         done
  370.         startup $ACTION $SCRIPTS
  371.     done
  372. fi
  373.  
  374. if [ S = "$runlevel" ]
  375. then
  376.     #
  377.     # For compatibility, run the files in /etc/rc.boot too.
  378.     #
  379.     [ -d /etc/rc.boot ] && run-parts /etc/rc.boot
  380. fi
  381.  
  382. trap - EXIT # Disable emergency handler
  383.  
  384. exit 0
  385.  
  386.